<?php
namespace Home\Model;
use Think\Model;
class MemberModel extends Model {
	public $table='think_member';
	public $page; //当前页
	public $perpage; //每页显示
	public $member_id; //ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词

	//提交
	public function user_add_post()
	{
		$Model=M("Member");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	//获得列表 $page当前页 $perpage每页显示 当前默认用户名 姓名
	public function get_user_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		$time=$this->time;

		$where = "where a.member_state=1 and a.member_id=b.member_id";
		if($time==1){
			$start_time=mktime(0,0,0,date('m'),date('d'),date('Y'));
			$end_time=time();
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==2){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$start_time=mktime(0,0,0,$month,($date-1),$year);
			$end_time=mktime(0,0,0,$month,$date,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==3){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$start_time=mktime(0,0,0,$month,($date-2),$year);
			$end_time=mktime(0,0,0,$month,($date-1),$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==4){
			$year = date('Y');
			$date=date('n');
			$month = (int)date('m');
			$week = (int)date('N');
			$end_time=mktime(0,0,0,$month,($date),$year);
			if(($date-$week)>0){
				$start_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$end = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week;
				$start_time=mktime(0,0,0,($month-1),$end,$year);
			}
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==5){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$week = (int)date('N');
			if(($date-$week)>0){
				$end_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$end = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week;
				$end_time=mktime(0,0,0,($month-1),$end,$year);
			}

			if(($date-$week-7)>0){
				$start_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$start = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week-7;
				$start_time=mktime(0,0,0,($month-1),$start,$year);
			}
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==6){
			$year = date('Y');
			$month = (int)date('m');
			$start_time=mktime(0,0,0,$month,1,$year);
			$end_time=mktime(0,0,0,($month+1),1,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==7){
			$year = date('Y');
			$month = (int)date('m');
			$start_time=mktime(0,0,0,($month-1),1,$year);
			$end_time=mktime(0,0,0,$month,1,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}
		//搜索

		$where='where a.member_state=1 and a.member_id=b.member_id ';
		if($this->condition){
			$where.=" and ".$this->condition;
		}
		$start=($page-1)*$perpage;
		$Model=new Model();

		$sql="SELECT a.*,b.* FROM `think_member` as a ,`think_company` as b ".$where." order by ".$this->order."  limit $start,$perpage";

		$list=$Model->query($sql);

		return $list;
	}
	//获得总数
	public function get_user_total($condition)
	{
		//搜索
		$time=$this->time;
		$where='where a.member_state=1 and a.member_id=b.member_id ';

		if($time==1){
			$start_time=mktime(0,0,0,date('m'),date('d'),date('Y'));
			$end_time=time();
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==2){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$start_time=mktime(0,0,0,$month,($date-1),$year);
			$end_time=mktime(0,0,0,$month,$date,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==3){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$start_time=mktime(0,0,0,$month,($date-2),$year);
			$end_time=mktime(0,0,0,$month,($date-1),$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==4){
			$year = date('Y');
			$date=date('n');
			$month = (int)date('m');
			$week = (int)date('N');
			$end_time=mktime(0,0,0,$month,($date),$year);
			if(($date-$week)>0){
				$start_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$end = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week;
				$start_time=mktime(0,0,0,($month-1),$end,$year);
			}
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==5){
			$year = date('Y');
			$date=date('j');
			$month = date('n');
			$week = (int)date('N');
			if(($date-$week)>0){
				$end_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$end = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week;
				$end_time=mktime(0,0,0,($month-1),$end,$year);
			}

			if(($date-$week-7)>0){
				$start_time=mktime(0,0,0,$month,($date-$week),$year);
			}else{
				$start = date('t',mktime(0, 0, 0,($month-1), 1, $year))+$date-$week-7;
				$start_time=mktime(0,0,0,($month-1),$start,$year);
			}
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==6){
			$year = date('Y');
			$month = (int)date('m');
			$start_time=mktime(0,0,0,$month,1,$year);
			$end_time=mktime(0,0,0,($month+1),1,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}else if($time==7){
			$year = date('Y');
			$month = (int)date('m');
			$start_time=mktime(0,0,0,($month-1),1,$year);
			$end_time=mktime(0,0,0,$month,1,$year);
			$where .= ' and a.member_reg_time>='.$start_time.' and a.member_reg_time<'.$end_time;
		}

		if($condition){
			$where.=" and ".$condition;
		}
		$Model=new Model();
		$sql = "select count(a.member_id) as total from ".$this->table." as a, think_company as b ".$where;
		//echo $sql;exit;
		$query=$Model->query($sql);
		return $query[0]['total'];
	}
	//获取信息
	public function get_user_info()
	{
		$member_id=intval($this->member_id);
		$Model=M("Member");
		$info=$Model->where('member_id='.$member_id)->find();
		if($info['admin_id']){
			$info['admin_name']=M('Admin')->where('admin_id='.$info['admin_id'])->getField('admin_name');
			$info['admin_truename']=M('Admin')->where('admin_id='.$info['admin_id'])->getField('admin_truename');
		}
		//print_r($info);exit;
		return $info;
	}
	//编辑提交
	public function user_edit_post()
	{
		$Model=M("Member");
		$condition['member_id'] = $this->post['member_id'];
		$data=$this->post;
		//print_r($this->post);exit;
		$result=$Model->where($condition)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_user()
	{
		$ids=$this->ids;
		$Model=M("Member");
		foreach($ids as $v){
			$condition['member_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}

	/**
	 * 会员数量
	 * @param array $condition
	 * @return int
	 */
	public function get_member_count() {
		$Model = M("Member");
		$condition = $this->condition;
		return $Model->where($condition)->count();
	}

	//获取所有用户列表，用于 添加供货信息
	public function get_member_list(){
		$Model=new Model();
		//$list=$Model->table('think_member a, think_company b')->where('a.member_id=b.member_id and a.member_state=1 and a.is_complete=1')->field('a.member_id as member_id, a.member_truename as member_truename, b.company_name as company_name')->order('a.member_id desc' )->select();
		$list=$Model->table('think_member a, think_company b')->where("a.member_id=b.member_id and a.member_state=1 and b.company_name!=''")->field('a.member_id as member_id, a.member_truename as member_truename, b.company_name as company_name')->order('a.member_sort asc,a.member_id desc' )->select();
		//dump($list);exit;
		return $list;
	}


	//首页统计，用于给客户展示
	public function get_index_member_total(){
		$Model=M('Member');
		$year = date('Y');
		$max=date('n');
		$month = (int)date('m');
		for($i=0;$i<$max;$i++){
			$start_time=mktime(0,0,0,($i+1),1,$year);
			$end_time=mktime(0,0,0,($i+2),1,$year);
			$data[$i]['label']=$year.'年/'.($i+1).'月';
			$data[$i]['value']=$Model->where("member_state=1 and member_reg_time2>=".$start_time." and member_reg_time2<".$end_time)->count();
		}
		return json_encode($data);
	}

	//月统计
	public function get_month_member_total(){
		$Model=M('Member');
		$year = date('Y');
		$max=date('n');
		$month = (int)date('m');
		for($i=0;$i<$max;$i++){
			$start_time=mktime(0,0,0,($i+1),1,$year);
			$end_time=mktime(0,0,0,($i+2),1,$year);
			$data[$i]['label']=$year.'年/'.($i+1).'月';
			$data[$i]['value']=$Model->where("member_state=1 and member_reg_time>=".$start_time." and member_reg_time<".$end_time)->count();
		}
		return json_encode($data);
	}

	//用于统计中的会员数据 最近7日数据
	public function get_day_member_total(){
		//echo date('Y-m-d H:i:s',time());exit;
		$Model=M('Member');
		//$start_time= date('m月d日', strtotime('-7 days'));
		$j=0;
		$today=mktime(0,0,0,date('m'),date('d'),date('Y'));
		//echo date('YmdHis',$today);exit;
		for($i=6;$i>=0;$i--){
			$start_time=$today-$i*24*60*60;
			$end_time=$today-($i-1)*24*60*60;
			/*if($i==0){
				echo date('YmdHis',$start_time).date('YmdHis',$end_time).'<br />';
			}*/
			$data[$j]['label']=date('n月j日', strtotime('-'.$i.' days'));
			$data[$j]['value']=$Model->where("member_state=1 and member_reg_time>=".$start_time." and member_reg_time<".$end_time)->count();
			//$data[$j]['start']=date('Y-m-d H:i:s', $start_time);
			//$data[$j]['end']=date('Y-m-d H:i:s', $end_time);
			$j++;
		}
		//dump($data);exit;
		return json_encode($data);
	}


	//判断邮箱是否存在 true表示存在
	public function check_email_exist()
	{
		$Model=M("Member");
		$where['member_email']=$this->post['member_email'];
		$count=$Model->where($where)->count();
		if($count==0){
			return false;
		}
		return true;
	}
	//判断是否存在 true表示存在
	public function check_mobile_exist()
	{
		$Model=M("Member");
		$where['member_mobile']=$this->post['member_mobile'];
		$count=$Model->where($where)->count();
		if($count==0){
			return false;
		}
		return true;
	}

	public function search_member_list(){
		$Model = M("Member");
		$search_word = $this->search_word;
		$where = "a.member_id=b.member_id and a.member_state=1 and b.company_name!='' and (b.company_name like '%".$search_word."%'  or a.member_truename  like '%".$search_word."%')";
		$list=$Model->table('think_member a, think_company b')->where($where)->field('a.member_id as member_id, a.member_truename as member_truename, b.company_name as company_name')->order('a.member_sort asc,a.member_id desc' )->select();
		return $list;
	}

}